﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      	MailMessage objEmail = new MailMessage();
        switch(DeliveryMethod.SelectedIndex)
        {
            case 0:
                objEmail.To.Add(new MailAddress(ToField.Text));
                break;
            case 1:
                objEmail.To.Add(new MailAddress(removeFormatting(ToField.Text) + "@teleflip.com"));
                break;
        }
        objEmail.From = new MailAddress(FromField.Text);
        objEmail.Subject = SubjectField.Text;
        objEmail.Body = BodyField.Text;
		// Make sure you have appropriate replying permissions from your local system
        SmtpClient smtp = new SmtpClient("mail.bryan800.com");

		try{
			smtp.Send(objEmail);
			Response.Write("Your Email has been sent sucessfully - Thank You");
		}
		catch (Exception exc){
			Response.Write("Send failure: " + exc.ToString());
		}
    }

    private string removeFormatting(string s)
    {
        char[] arr = s.ToCharArray();
        int removed = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if ((int)arr[i] > 57 || (int)arr[i] < 48)
            {
                s.Remove(i - removed);
                removed++;
            }
        }
        return arr.ToString();
    }
}
